package com.overseasShopping.Action;

import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.overseasShopping.Biz.ChatBiz;
import com.overseasShopping.pojo.Chat;
import com.overseasShopping.pojo.User;

public class ChatAction extends ActionSupport{
	private String userFrom;
	private String userTo;
	private String content;
	private ChatBiz chatBiz;
	
	
	public String getUserFrom() {
		return userFrom;
	}
	public void setUserFrom(String userFrom) {
		this.userFrom = userFrom;
	}
	public String getUserTo() {
		return userTo;
	}
	public void setUserTo(String userTo) {
		this.userTo = userTo;
	}
	public ChatBiz getChatBiz() {
		return chatBiz;
	}
	public void setChatBiz(ChatBiz chatBiz) {
		this.chatBiz = chatBiz;
	}
	public ChatAction() {
		super();
	}
	
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String addChat() throws Exception {
		//设置客服id
		userTo="a10001";
//		Map<String, Object>map=ActionContext.getContext().getSession();
//		User user=(User) map.get("user");
//		String userId=user.getUserId();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		///添加聊天记录
		chatBiz.addChat("s10001","a10001",content);
		return null;
	}
	
	//用户获取最新的聊天记录
	public String getChat() throws Exception {
//		Map<String, Object>map=ActionContext.getContext().getSession();
//		User user=(User) map.get("user");
//		String userId=user.getUserId();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter pw=response.getWriter();
		Chat chat=chatBiz.getChat("a10001","s10001");
		
		if(chat==null||chat.getcontent().equals("[endchat]")){
			pw.print("fail");
			
		}else{
			pw.print(chat);
		}
		pw.flush();
		pw.close();
	return null;
	}
	
	//客服获取最新的聊天记录
		public String getChat2() throws Exception {
//			Map<String, Object>map=ActionContext.getContext().getSession();
//			User user=(User) map.get("user");
//			String userId=user.getUserId();
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			PrintWriter pw=response.getWriter();
			Chat chat=chatBiz.getChat("s10001","a10001");
			if(chat==null||chat.getcontent().equals("[endchat]")){
				pw.print("fail");
				
			}else{
				pw.print(chat);
			}
			pw.flush();
			pw.close();
		return null;
		}
		
		//客服获取添加的聊天记录
		public String addChat2() throws Exception {
//			Map<String, Object>map=ActionContext.getContext().getSession();
//			User user=(User) map.get("user");
//			String userId=user.getUserId();
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			///添加聊天记录
			chatBiz.addChat("a10001","s10001",content);
			return null;
		}
}
